﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZY.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZY.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:View_Tab_Userinfo
    /// </summary>
    public partial class View_Tab_Userinfo : IView_Tab_Userinfo
    {
        public View_Tab_Userinfo()
        { }
        #region  BasicMethod


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZY.Model.View_Tab_Userinfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into View_Tab_Userinfo(");
            strSql.Append("pid,pname,rid,rname,dp_id,dname,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,ustats,u_img,u_leve,uleves,u_sn,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4)");
            strSql.Append(" values (");
            strSql.Append("@pid,@pname,@rid,@rname,@dp_id,@dname,@u_userName,@u_password,@u_trueName,@u_power,@u_mobilePhone,@u_email,@u_address,@u_state,@ustats,@u_img,@u_leve,@uleves,@u_sn,@u_createtime,@u_lastime,@u_ext1,@u_ext2,@u_ext3,@u_ext4)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@pid", SqlDbType.Int,4),
					new SqlParameter("@pname", SqlDbType.VarChar,50),
					new SqlParameter("@rid", SqlDbType.Int,4),
					new SqlParameter("@rname", SqlDbType.VarChar,50),
					new SqlParameter("@dp_id", SqlDbType.Int,4),
					new SqlParameter("@dname", SqlDbType.VarChar,50),
					new SqlParameter("@u_userName", SqlDbType.VarChar,50),
					new SqlParameter("@u_password", SqlDbType.VarChar,50),
					new SqlParameter("@u_trueName", SqlDbType.VarChar,50),
					new SqlParameter("@u_power", SqlDbType.VarChar,8000),
					new SqlParameter("@u_mobilePhone", SqlDbType.VarChar,50),
					new SqlParameter("@u_email", SqlDbType.VarChar,250),
					new SqlParameter("@u_address", SqlDbType.VarChar,250),
					new SqlParameter("@u_state", SqlDbType.Int,4),
					new SqlParameter("@ustats", SqlDbType.VarChar,4),
					new SqlParameter("@u_img", SqlDbType.VarChar,5000),
					new SqlParameter("@u_leve", SqlDbType.Int,4),
					new SqlParameter("@uleves", SqlDbType.VarChar,8),
					new SqlParameter("@u_sn", SqlDbType.Int,4),
					new SqlParameter("@u_createtime", SqlDbType.DateTime),
					new SqlParameter("@u_lastime", SqlDbType.DateTime),
					new SqlParameter("@u_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext4", SqlDbType.VarChar,50)};
            parameters[0].Value = model.pid;
            parameters[1].Value = model.pname;
            parameters[2].Value = model.rid;
            parameters[3].Value = model.rname;
            parameters[4].Value = model.dp_id;
            parameters[5].Value = model.dname;
            parameters[6].Value = model.u_userName;
            parameters[7].Value = model.u_password;
            parameters[8].Value = model.u_trueName;
            parameters[9].Value = model.u_power;
            parameters[10].Value = model.u_mobilePhone;
            parameters[11].Value = model.u_email;
            parameters[12].Value = model.u_address;
            parameters[13].Value = model.u_state;
            parameters[14].Value = model.ustats;
            parameters[15].Value = model.u_img;
            parameters[16].Value = model.u_leve;
            parameters[17].Value = model.uleves;
            parameters[18].Value = model.u_sn;
            parameters[19].Value = model.u_createtime;
            parameters[20].Value = model.u_lastime;
            parameters[21].Value = model.u_ext1;
            parameters[22].Value = model.u_ext2;
            parameters[23].Value = model.u_ext3;
            parameters[24].Value = model.u_ext4;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZY.Model.View_Tab_Userinfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update View_Tab_Userinfo set ");
            strSql.Append("pid=@pid,");
            strSql.Append("pname=@pname,");
            strSql.Append("rid=@rid,");
            strSql.Append("rname=@rname,");
            strSql.Append("dp_id=@dp_id,");
            strSql.Append("dname=@dname,");
            strSql.Append("u_userName=@u_userName,");
            strSql.Append("u_password=@u_password,");
            strSql.Append("u_trueName=@u_trueName,");
            strSql.Append("u_power=@u_power,");
            strSql.Append("u_mobilePhone=@u_mobilePhone,");
            strSql.Append("u_email=@u_email,");
            strSql.Append("u_address=@u_address,");
            strSql.Append("u_state=@u_state,");
            strSql.Append("ustats=@ustats,");
            strSql.Append("u_img=@u_img,");
            strSql.Append("u_leve=@u_leve,");
            strSql.Append("uleves=@uleves,");
            strSql.Append("u_sn=@u_sn,");
            strSql.Append("u_createtime=@u_createtime,");
            strSql.Append("u_lastime=@u_lastime,");
            strSql.Append("u_ext1=@u_ext1,");
            strSql.Append("u_ext2=@u_ext2,");
            strSql.Append("u_ext3=@u_ext3,");
            strSql.Append("u_ext4=@u_ext4");
            strSql.Append(" where uid=@uid");
            SqlParameter[] parameters = {
					new SqlParameter("@pid", SqlDbType.Int,4),
					new SqlParameter("@pname", SqlDbType.VarChar,50),
					new SqlParameter("@rid", SqlDbType.Int,4),
					new SqlParameter("@rname", SqlDbType.VarChar,50),
					new SqlParameter("@dp_id", SqlDbType.Int,4),
					new SqlParameter("@dname", SqlDbType.VarChar,50),
					new SqlParameter("@u_userName", SqlDbType.VarChar,50),
					new SqlParameter("@u_password", SqlDbType.VarChar,50),
					new SqlParameter("@u_trueName", SqlDbType.VarChar,50),
					new SqlParameter("@u_power", SqlDbType.VarChar,8000),
					new SqlParameter("@u_mobilePhone", SqlDbType.VarChar,50),
					new SqlParameter("@u_email", SqlDbType.VarChar,250),
					new SqlParameter("@u_address", SqlDbType.VarChar,250),
					new SqlParameter("@u_state", SqlDbType.Int,4),
					new SqlParameter("@ustats", SqlDbType.VarChar,4),
					new SqlParameter("@u_img", SqlDbType.VarChar,5000),
					new SqlParameter("@u_leve", SqlDbType.Int,4),
					new SqlParameter("@uleves", SqlDbType.VarChar,8),
					new SqlParameter("@u_sn", SqlDbType.Int,4),
					new SqlParameter("@u_createtime", SqlDbType.DateTime),
					new SqlParameter("@u_lastime", SqlDbType.DateTime),
					new SqlParameter("@u_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@u_ext4", SqlDbType.VarChar,50),
					new SqlParameter("@uid", SqlDbType.Int,4)};
            parameters[0].Value = model.pid;
            parameters[1].Value = model.pname;
            parameters[2].Value = model.rid;
            parameters[3].Value = model.rname;
            parameters[4].Value = model.dp_id;
            parameters[5].Value = model.dname;
            parameters[6].Value = model.u_userName;
            parameters[7].Value = model.u_password;
            parameters[8].Value = model.u_trueName;
            parameters[9].Value = model.u_power;
            parameters[10].Value = model.u_mobilePhone;
            parameters[11].Value = model.u_email;
            parameters[12].Value = model.u_address;
            parameters[13].Value = model.u_state;
            parameters[14].Value = model.ustats;
            parameters[15].Value = model.u_img;
            parameters[16].Value = model.u_leve;
            parameters[17].Value = model.uleves;
            parameters[18].Value = model.u_sn;
            parameters[19].Value = model.u_createtime;
            parameters[20].Value = model.u_lastime;
            parameters[21].Value = model.u_ext1;
            parameters[22].Value = model.u_ext2;
            parameters[23].Value = model.u_ext3;
            parameters[24].Value = model.u_ext4;
            parameters[25].Value = model.uid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int uid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from View_Tab_Userinfo ");
            strSql.Append(" where uid=@uid");
            SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.Int,4)
			};
            parameters[0].Value = uid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string uidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from View_Tab_Userinfo ");
            strSql.Append(" where uid in (" + uidlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZY.Model.View_Tab_Userinfo GetModel(int uid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 uid,pid,pname,rid,rname,dp_id,dname,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,ustats,u_img,u_leve,uleves,u_sn,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4 from View_Tab_Userinfo ");
            strSql.Append(" where uid=@uid");
            SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.Int,4)
			};
            parameters[0].Value = uid;

            ZY.Model.View_Tab_Userinfo model = new ZY.Model.View_Tab_Userinfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZY.Model.View_Tab_Userinfo DataRowToModel(DataRow row)
        {
            ZY.Model.View_Tab_Userinfo model = new ZY.Model.View_Tab_Userinfo();
            if (row != null)
            {
                if (row["uid"] != null && row["uid"].ToString() != "")
                {
                    model.uid = int.Parse(row["uid"].ToString());
                }
                if (row["pid"] != null && row["pid"].ToString() != "")
                {
                    model.pid = int.Parse(row["pid"].ToString());
                }
                if (row["pname"] != null)
                {
                    model.pname = row["pname"].ToString();
                }
                if (row["rid"] != null && row["rid"].ToString() != "")
                {
                    model.rid = int.Parse(row["rid"].ToString());
                }
                if (row["rname"] != null)
                {
                    model.rname = row["rname"].ToString();
                }
                if (row["dp_id"] != null && row["dp_id"].ToString() != "")
                {
                    model.dp_id = int.Parse(row["dp_id"].ToString());
                }
                if (row["dname"] != null)
                {
                    model.dname = row["dname"].ToString();
                }
                if (row["u_userName"] != null)
                {
                    model.u_userName = row["u_userName"].ToString();
                }
                if (row["u_password"] != null)
                {
                    model.u_password = row["u_password"].ToString();
                }
                if (row["u_trueName"] != null)
                {
                    model.u_trueName = row["u_trueName"].ToString();
                }
                if (row["u_power"] != null)
                {
                    model.u_power = row["u_power"].ToString();
                }
                if (row["u_mobilePhone"] != null)
                {
                    model.u_mobilePhone = row["u_mobilePhone"].ToString();
                }
                if (row["u_email"] != null)
                {
                    model.u_email = row["u_email"].ToString();
                }
                if (row["u_address"] != null)
                {
                    model.u_address = row["u_address"].ToString();
                }
                if (row["u_state"] != null && row["u_state"].ToString() != "")
                {
                    model.u_state = int.Parse(row["u_state"].ToString());
                }
                if (row["ustats"] != null)
                {
                    model.ustats = row["ustats"].ToString();
                }
                if (row["u_img"] != null)
                {
                    model.u_img = row["u_img"].ToString();
                }
                if (row["u_leve"] != null && row["u_leve"].ToString() != "")
                {
                    model.u_leve = int.Parse(row["u_leve"].ToString());
                }
                if (row["uleves"] != null)
                {
                    model.uleves = row["uleves"].ToString();
                }
                if (row["u_sn"] != null && row["u_sn"].ToString() != "")
                {
                    model.u_sn = int.Parse(row["u_sn"].ToString());
                }
                if (row["u_createtime"] != null && row["u_createtime"].ToString() != "")
                {
                    model.u_createtime = DateTime.Parse(row["u_createtime"].ToString());
                }
                if (row["u_lastime"] != null && row["u_lastime"].ToString() != "")
                {
                    model.u_lastime = DateTime.Parse(row["u_lastime"].ToString());
                }
                if (row["u_ext1"] != null)
                {
                    model.u_ext1 = row["u_ext1"].ToString();
                }
                if (row["u_ext2"] != null)
                {
                    model.u_ext2 = row["u_ext2"].ToString();
                }
                if (row["u_ext3"] != null)
                {
                    model.u_ext3 = row["u_ext3"].ToString();
                }
                if (row["u_ext4"] != null)
                {
                    model.u_ext4 = row["u_ext4"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uid,pid,pname,rid,rname,dp_id,dname,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,ustats,u_img,u_leve,uleves,u_sn,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4 ");
            strSql.Append(" FROM View_Tab_Userinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" uid,pid,pname,rid,rname,dp_id,dname,u_userName,u_password,u_trueName,u_power,u_mobilePhone,u_email,u_address,u_state,ustats,u_img,u_leve,uleves,u_sn,u_createtime,u_lastime,u_ext1,u_ext2,u_ext3,u_ext4 ");
            strSql.Append(" FROM View_Tab_Userinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM View_Tab_Userinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.uid desc");
            }
            strSql.Append(")AS Row, T.*  from View_Tab_Userinfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "View_Tab_Userinfo";
            parameters[1].Value = "uid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

